/********************************************************************

Write Date: 2011/06/02
Author: Deng Nianchen
>>>> Add class OOPString to provide a char/wchar compatible string

*********************************************************************/
#include "OOPString.h"

using namespace OOPUtils;
using namespace std;


OOPString::OOPString () : wstring () {}

OOPString::OOPString (const OOPString & _Right)
    : wstring (_Right) {}

OOPString::OOPString (const _Alloc & _Al) : wstring (_Al) {}

OOPString::OOPString (const OOPString & _Right,
    size_type _Roff, size_type _Count) : wstring (_Right, _Roff, _Count) {}

OOPString::OOPString (const OOPString & _Right,
    size_type _Roff, size_type _Count, const _Alloc & _Al)
    : wstring (_Right, _Roff, _Count, _Al) {}

OOPString::OOPString (const wchar_t * _Ptr, size_type _Count)
    : wstring (_Ptr, _Count) {}

OOPString::OOPString (const wchar_t * _Ptr, size_type _Count,
    const _Alloc & _Al) : wstring (_Ptr, _Count, _Al) {}

OOPString::OOPString (const wchar_t * _Ptr)
    : wstring (_Ptr) {}

OOPString::OOPString (const wchar_t * _Ptr, const _Alloc & _Al)
    : wstring (_Ptr, _Al) {}

OOPString::OOPString (size_type _Count, wchar_t _Ch)
    : wstring (_Count, _Ch) {}

OOPString::OOPString (size_type _Count, wchar_t _Ch,
    const _Alloc & _Al) : wstring (_Count, _Ch, _Al) {}

OOPString::OOPString (const_pointer _First, const_pointer _Last)
    : wstring(_First, _Last) {}

OOPString::OOPString (const_pointer _First, const_pointer _Last,
    const _Alloc & _Al) : wstring (_First, _Last, _Al) {}

OOPString::OOPString (const_iterator _First,
    const_iterator _Last) : wstring (_First, _Last) {}

OOPString::OOPString (OOPString && _Right) : wstring (_Right) {}

string OOPString::toStdString () const
{
	std::string curLocale = setlocale (LC_ALL, NULL);        // curLocale = "C";
	setlocale (LC_ALL, "chs");
	size_t strSize = 2 * size() + 1;
	char * cString = new char[strSize];
	memset (cString, 0, strSize);
	wcstombs (cString, c_str (), strSize);
	std::string result = cString;
	delete [] cString;
	setlocale (LC_ALL, curLocale.c_str());
	return result;
}

wstring OOPString::toStdWString () const
{
  return *this;
}

OOPString OOPUtils::OOPString::fromStdString (const std::string & str)
{
	std::string curLocale = setlocale (LC_ALL, NULL);        // curLocale = "C";
	setlocale (LC_ALL, "chs"); 
	size_t _Dsize = str.size () + 1;
	wchar_t *_Dest = new wchar_t[_Dsize];
	wmemset(_Dest, 0, _Dsize);
	mbstowcs(_Dest, str.c_str(), _Dsize);
	OOPString result = _Dest;
	delete [] _Dest;
	setlocale (LC_ALL, curLocale.c_str ());
	return result;
}

OOPString OOPUtils::OOPString::fromStdWString (const std::wstring & str)
{
	OOPString oopStr;
	(std::wstring)oopStr = str;
	return oopStr;
}

ostream & operator<< (ostream & outStream, const OOPString & str)
{
  outStream << str.toStdString ();
  return outStream;
}

istream & operator>> (istream & inStream, OOPString & str)
{
  string stdString;
  inStream >> stdString;
  str = OOPString::fromStdString (stdString);
  return inStream;
}
